<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="../include/admincontent.jsp" %>
<div id="location">当前位置：内容管理 > 栏目管理</div>
<div id="Content">
    <form id="Validform" method="post" action="/CMS/NodeAction">
        <div class="help"><b>提示：</b>删除栏目时会将该栏目下的文章删除，请谨慎操作。</div>
        <div class="toolbars">
            <a id="save">
                <em class="ico16 save_traft_16"></em>
                <span class="menu_span" title="保存">保存</span>
            </a>
            <a id="add">
                <em class="ico16 add_new"></em>
                <span class="menu_span" title="添加">添加</span>
            </a>
        </div>
        <table id="nodeTable" class="treeTable inputform" style="margin-top: 10px;width: 1040px">
            <thead>
            <tr>
                <td width="60">排序号</td>
                <td width="200">栏目名称</td>
                <td width="120">显示到前台菜单</td>
                <td width="200">内容显示方式</td>
                <td width="60"></td>
            </tr>
            </thead>
            <tbody>
            ${result}
            </tbody>
        </table>
    </form>
</div>
<script type="text/javascript">

    $(document).ready(function () {
        /*        $('.treeTable button.add').click(function () {
                    var str = '<tr><td><div><input type="text" datatype="*" nullmsg="栏目排序号不能为空！"><input type="hidden"></div></td><td><div><input type="text" datatype="*" nullmsg="栏目名称不能为空！"></div></td><td><div><select datatype="*" nullmsg="显示到前台菜单不能为空！"><option value="0">显示</option><option value="1">不显示</option></select></div></td><td><div><select datatype="*" nullmsg="内容显示方式不能为空！"><option value="0">默认</option><option value="1">显示最新发布的一条信息</option></select></div></td><td><button type="button" class="del"></button></td></tr>';
                    $('.treeTable tbody').append(str);
                    setTableInfo();
                });*/
        //nodeThis = this;
        $('#add').click(function () {
            var str = '<tr><td><div><input type="text" datatype="*" nullmsg="栏目排序号不能为空！"><input type="hidden"></div></td><td><div><input type="text" datatype="*" nullmsg="栏目名称不能为空！"></div></td><td><div><select datatype="*" nullmsg="显示到前台菜单不能为空！"><option value="0">显示</option><option value="1">不显示</option></select></div></td><td><div><select datatype="*" nullmsg="内容显示方式不能为空！"><option value="0">默认</option><option value="1">显示最新发布的一条信息</option></select></div></td>' +
                '<td class="tdwidth"><button type="button"  class="layui-btn layui-btn-sm layui-btn-normal del"><i class="layui-icon"></i> 删除</button></td></tr>';
            $('.treeTable tbody').append(str);
            setTableInfo();
        });

        $('#save').click(function () {
            var rows = nodeTable.rows.length;
            var data = {};
            for (var i = 0; i < rows - 1; i++) {
                var nodeID = $("input[name='node[" + i + "].id']").val()
                var nodeIdName = "node[" + i + "].id";
                data[nodeIdName] = nodeID;
                var nodeOrdernum = $("input[name='node[" + i + "].ordernum']").val();
                if (nodeOrdernum == "") {
                    layer.msg("序列号不能空", {icon: 2});
                    return;
                }
                var nodeOrdernumName = "node[" + i + "].ordernum";
                data[nodeOrdernumName] = nodeOrdernum;
                var nodeName = $("input[name='node[" + i + "].nodename']").val();
                if (nodeOrdernum == "") {
                    layer.msg("栏目名称不能为空", {icon: 2});
                    return;
                }
                var nodeNameName = "node[" + i + "].nodename";
                data[nodeNameName] = nodeName;
                var nodeState = $("input[name='node[" + i + "].state']").val();
                if (nodeState == null)
                    nodeState = 0;
                var nodeStateName = "node[" + i + "].state";
                data[nodeStateName] = nodeState;
                var nodeType = $("input[name='node[" + i + "].type']").val();
                if (nodeType == null)
                    nodeType = 0;
                var nodeTypeName = "node[" + i + "].type";
                data[nodeTypeName] = nodeType;

            }

            $.ajax({
                url: "/CMS/NodeAction",
                type: 'POST',
                data: data,
                success: function (data) {
                    layer.msg(data.Message, {icon: 1});
                    //self.location = document.referrer;
                },
                error: function (data) {
                    layer.msg(data.Message, {icon: 2});
                }
            });

        });

        $(document).delegate('.treeTable button.del', 'click', function () {
            var obj = $(this).parent().parent();
            var id = obj.find('td:first input[type=hidden]').val();
            if (id.length > 0) {
                postAjax('/CMS/DelNodeAction?id=' + id, null, function (data) {
                    if (data.state) {
                        obj.remove();
                        setTableInfo();
                    }
                    layer.msg(data.Message, {icon: 2});
                });
            } else {
                obj.remove();
                setTableInfo();
            }
        });
        $('select').each(function () {
            if ($(this).next()) {
                $(this).val($(this).next().val());
            }
        });
    });

    function setTableInfo() {
        $('.treeTable tbody tr').each(function (index) {
            $(this).find('td:eq(0) input[type=text]').attr('name', 'node[' + index + '].ordernum');
            $(this).find('td:eq(0) input[type=hidden]').attr('name', 'node[' + index + '].id');
            $(this).find('td:eq(1) input').attr('name', 'node[' + index + '].nodename');
            $(this).find('td:eq(2) select').attr('name', 'node[' + index + '].state');
            $(this).find('td:eq(3) select').attr('name', 'node[' + index + '].type');
        });
    }
</script>
</body>
<link rel="stylesheet" href="<%=basepath%>/resources/css/skin.css"/>
</html>